#!groovy
pipeline {
  agent any
  stages {
    stage('初始化') {
      steps {
        echo "【信息】打印环境变量初始值"
        sh "printenv"

        echo "【信息】Job全部参数：${params}"

        script {
          Common = load './common/common.groovy'
          Common.applicationRootAbsolute = Common.getApplicationRootAbsolute(params.APPLICATION_ROOT_DIR)
        }

        // 拉取部署文件
        dir(Common.pipelineCicdDir) {
          git env.GIT_URL
        }

        // 拉取应用代码，需要安装插件git parameter
        git branch: params.APPLICATION_GIT_BRANCH, url: params.APPLICATION_GIT_URL

        dir(Common.applicationRootAbsolute) {
          script {
            Common.cicdYamlData = Common.readCicdYaml(params.ENV_OPTION, Common.applicationRootAbsolute)
          }
        }
      }
    }

    stage('编译') {
      steps {
        dir(Common.applicationRootAbsolute) {
          echo "【信息】开始编译"
          sh """
            if [ ${params.INSTALL} == true ];then
                cnpm install
            fi
            cnpm run build
          """
        }
      }
    }

    stage('构建Docker镜像') {
      steps {
        // 拷贝docker打包文件、jar包到docker-image
        dir(Common.applicationRootAbsolute) {
          sh """
            mkdir -p docker-image
            rm -rf docker-image/*
            /bin/cp -rf dist docker-image/dist
          """
          script {
            if (Common.cicdYamlData.dockerfile) {
              // 使用了自定义的Dockerfile
              sh "/bin/cp -rf cicd/* docker-image/"
            } else {
              // 未使用自定义的Dockerfile
              sh "/bin/cp -rf ${Common.pipelineCicdDir}/template/vue/Dockerfile docker-image/Dockerfile"
            }
          }
        }
        // 构建镜像
        dir("${Common.applicationRootAbsolute}/docker-image") {
          script {
            def nowTime = new Date().format("yyyy-MM-dd HH:mm:ss")
            nowTime = "d" + nowTime.replace(" ", "t").replace(":", "-")
            def dockerImage = String.format("%s/%s/%s-%s:%s",
                Common.insecureRegistry, Common.cicdYamlData.imageGroup,
                Common.cicdYamlData.applicationName, Common.cicdYamlData.envData.envName, nowTime)
            def dockerImageLatest = String.format("%s/%s/%s-%s:latest",
                Common.insecureRegistry, Common.cicdYamlData.imageGroup,
                Common.cicdYamlData.applicationName, Common.cicdYamlData.envData.envName)
            sh """
              docker build -t ${dockerImage} .
              docker push ${dockerImage}

              docker tag ${dockerImage} ${dockerImageLatest}
              docker push ${dockerImageLatest}

              docker rmi ${dockerImage} ${dockerImageLatest}
            """
            println "【信息】############当前镜像名称是${dockerImage}"
          }
        }
      }
    }
  }
  post {
    success {
      script {
        if (Common.cicdYamlData.notice.wx.ciSuccessSend || Common.cicdYamlData.notice.wx.cdSuccessSend) {
          Common.curlWx(Common.cicdYamlData.notice.wx.key, env.JOB_NAME, null, true)
        }
      }
    }
    failure {
      script {
        if (Common.cicdYamlData.notice.wx.ciFailureSend || Common.cicdYamlData.notice.wx.cdFailureSend) {
          Common.curlWx(Common.cicdYamlData.notice.wx.key, env.JOB_NAME, null, false)
        }
      }
    }
  }
}

